﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using be.brandweernijlen.adembescherming.Controllers;
using System.Threading;
using System.IO;
using System.Diagnostics;

namespace be.brandweernijlen.adembescherming
{
    public partial class FrmMain : Form
    {
        DbController dbController;
        public FrmMain()
        {
            InitializeComponent();

        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            
            Boolean isValid = true;
            foreach(DataGridViewRow row in dgvRows.Rows)
            {
              
                    if (row.ErrorText.Length > 0)
                    {
                        isValid = false;
                    }
               
            }

            if (dgvRows.Rows.Count == 0)
                isValid = false;


            if (!isValid)
            {
                MessageBox.Show("Er zijn geen rijen ingevuld of er zijn nog errors aanwezig.");
                return;
            }

            if (MessageBox.Show("Ben je zeker?", "Toevoegen", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                return;

            if (chkBackup.Checked)
            {
                String newFileName = ("db" + " " + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + " " + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".accdb");
                File.Copy("db.accdb", newFileName );
            }

            dbDataSet.TBL_ABDOCRow newRow = dbController.createNewAbDoc(dbDataSet);

            tbL_ABDOCTableAdapter1.Update(dbDataSet);

            int abDocId = (int)tableAdapterManager.TBL_ABDOCTableAdapter.getLastId();
           

            foreach(DataGridViewRow row in dgvRows.Rows)
            {
                int number = int.Parse(row.Cells[0].Value.ToString());
                int pressure = int.Parse(row.Cells[1].Value.ToString());
                int person = int.Parse(row.Cells[2].ToolTipText.ToString());
                DateTime date = DateTime.ParseExact((String)row.Cells[3].Value, "d/MM/yyyy",
                                  Thread.CurrentThread.CurrentCulture);
                dbController.createNewAbDocRow(dbDataSet, abDocId, number,  pressure,  person, date);
                tbL_ABDOCSTATUSTableAdapter1.Update(dbDataSet);
            }

            PDFController.generatePDF(tableAdapterManager);

            dgvRows.Rows.Clear();

            Process.Start("temp.pdf");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dbController = new DbController();
            this.tBL_PERSONTableAdapter.Fill(this.dbDataSet.TBL_PERSON);
            this.tbL_ABDOCTableAdapter1.Fill(this.dbDataSet.TBL_ABDOC);
            this.tbL_ABDOCSTATUSTableAdapter1.Fill(this.dbDataSet.TBL_ABDOCSTATUS);
            cbResponsible.SelectedIndex = 0;

        }

       
        private void btnAddRow_Click(object sender, EventArgs e)
        {
            foreach (String numberString in clbNumber.CheckedItems)
            {
                DataGridViewRow row = new DataGridViewRow();
                DataGridViewTextBoxCell number = new DataGridViewTextBoxCell();
                DataGridViewTextBoxCell pressure = new DataGridViewTextBoxCell();
                DataGridViewTextBoxCell responsible = new DataGridViewTextBoxCell();
                DataGridViewTextBoxCell date = new DataGridViewTextBoxCell();

                number.Value = numberString;
                pressure.Value = 270;
                responsible.Value = cbResponsible.Text;
                responsible.ToolTipText = cbResponsible.SelectedValue.ToString();
                date.Value = dtpDate.Value.ToShortDateString();

                row.Cells.Add(number);
                row.Cells.Add(pressure);
                row.Cells.Add(responsible);
                row.Cells.Add(date);

                dgvRows.Rows.Add(row);
            }
        }

        private void dgvRows_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex.Equals(1))
            {
                {
                    String textWaarde = ((DataGridView)sender).CurrentCell.Value.ToString();
                    int waarde = int.Parse(textWaarde);

                    if ((waarde > 330 || waarde < 270) && !dgvRows[e.ColumnIndex, e.RowIndex].IsInEditMode)
                    {
                        dgvRows.Rows[e.RowIndex].ErrorText = "Verkeerde waarde. Deze moet tussen 270 & 330 vallen.";
                    }
                    else
                    {
                        dgvRows.Rows[e.RowIndex].ErrorText = String.Empty;
                    }
                }
            }
        }

        private void btnGenerateByBottle_Click(object sender, EventArgs e)
        {
            PDFController.generatePDFByBottle(tableAdapterManager, lstNumbersForBottles.CheckedItems);


            Process.Start("temp.pdf");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PDFController.generatePDF(tableAdapterManager);

            Process.Start("temp.pdf");
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
    }
}
